<?php

/* @var $this yii\web\View */

$this->title = '设备列表';
?>
<style>
    #addDevice {
        position: absolute;
        top: -38px;
        right: 20%;
    }
    input.has-error {
        border-color: red !important;
    }
</style>
<h3><?= $this->title ?></h3>
<div class="site-index" style="position: relative">
    <button type="button" class="btn btn-primary" id="addDevice" data-toggle="modal" data-target="#addModal">添加设备
    </button>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>设备id</th>
            <th>设备名称</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($list as $device): ?>
            <tr>
                <td><?= $device['deviceId'] ?></td>
                <td><?= $device['deviceName'] ?></td>
                <td>
                    <a target="_blank" class="btn btn-info"
                       href="/site/info?deviceId=<?= $device['deviceId'] ?>&deviceName=<?= urlencode($device['deviceName']) ?>">查看信息
                    </a>
                    <a target="_blank" style="cursor: pointer" class="btn btn-info"
                       onclick="delDevice('<?= $device['deviceId'] ?>', '<?= $device['deviceName'] ?>')">
                        删除
                    </a>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
<?= $this->render('add_modal.php', ['list' => $list]) ?>
<script>
    function delDevice(deviceId, name) {
        if (confirm('确定要删除设备' + name + '？')) {
            $.getJSON('/site/del-device?deviceId=' + deviceId, [], function (data) {
                if (data.status === 1) {
                    $.showSuccessTimeout(data.message, function () {
                        location.reload();
                    });
                } else {
                    $.showErr(data.message);
                }
            })
        }
    }
</script>